{% extends "base.html" %}

{% block title %}消息中心{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="card-title mb-0">消息中心</h4>
                    <div>
                        <a href="{{ url_for('message.compose_message') }}" class="btn btn-primary">
                            <i class="fas fa-plus"></i> 撰写消息
                        </a>
                        {% if current_user.role == 'admin' %}
                        <a href="{{ url_for('message.message_templates') }}" class="btn btn-info ml-2">
                            <i class="fas fa-cog"></i> 消息模板
                        </a>
                        {% endif %}
                    </div>
                </div>
                <div class="card-body">
                    <!-- 消息筛选标签 -->
                    <ul class="nav nav-tabs mb-3">
                        <li class="nav-item">
                            <a class="nav-link {% if not unread_only %}active{% endif %}" 
                               href="{{ url_for('message.message_center', unread_only='false') }}">
                                全部消息
                                {% if unread_count > 0 %}
                                <span class="badge badge-primary ml-1">{{ unread_count }}</span>
                                {% endif %}
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if unread_only %}active{% endif %}" 
                               href="{{ url_for('message.message_center', unread_only='true') }}">
                                未读消息
                                {% if unread_count > 0 %}
                                <span class="badge badge-danger ml-1">{{ unread_count }}</span>
                                {% endif %}
                            </a>
                        </li>
                    </ul>
                    
                    <!-- 操作按钮 -->
                    <div class="mb-3">
                        <button id="mark-all-read" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-check-double"></i> 全部标记为已读
                        </button>
                    </div>
                    
                    <!-- 消息列表 -->
                    <div class="message-list">
                        {% if messages.items %}
                        {% for message in messages.items %}
                        <div class="card mb-2 message-item {% if not message.is_read %}unread{% endif %}" 
                             data-message-id="{{ message.id }}">
                            <div class="card-body py-2">
                                <div class="d-flex justify-content-between align-items-center">
                                    <div class="d-flex align-items-center">
                                        {% if not message.is_read %}
                                        <i class="fas fa-circle text-primary mr-2" style="font-size: 8px;"></i>
                                        {% endif %}
                                        <div>
                                            <h6 class="mb-1">
                                                <a href="{{ url_for('message.view_message', message_id=message.id) }}" 
                                                   class="text-dark">
                                                    {{ message.title }}
                                                </a>
                                            </h6>
                                            <p class="mb-1 text-muted small">
                                                发送者: {{ message.sender.name if message.sender else '系统' }} | 
                                                类型: {{ message.message_type }} | 
                                                时间: {{ message.created_at.strftime('%Y-%m-%d %H:%M') }}
                                            </p>
                                        </div>
                                    </div>
                                    <div>
                                        <span class="badge badge-{{ 
                                            'danger' if message.priority == '紧急' 
                                            else 'warning' if message.priority == '高' 
                                            else 'info' if message.priority == '普通' 
                                            else 'secondary' 
                                        }}">
                                            {{ message.priority }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                        {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                            <p class="text-muted">暂无{{ '未读' if unread_only else '' }}消息</p>
                        </div>
                        {% endif %}
                    </div>
                    
                    <!-- 分页 -->
                    {% if messages.pages > 1 %}
                    <nav aria-label="消息列表分页">
                        <ul class="pagination justify-content-center">
                            {% if messages.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('message.message_center', page=messages.prev_num, unread_only=unread_only) }}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            {% endif %}
                            
                            {% for page_num in messages.iter_pages() %}
                            {% if page_num %}
                            {% if page_num != messages.page %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('message.message_center', page=page_num, unread_only=unread_only) }}">
                                    {{ page_num }}
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item active">
                                <span class="page-link">{{ page_num }}</span>
                            </li>
                            {% endif %}
                            {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                            {% endif %}
                            {% endfor %}
                            
                            {% if messages.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('message.message_center', page=messages.next_num, unread_only=unread_only) }}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
    // 全部标记为已读
    $('#mark-all-read').click(function() {
        if (confirm('确定要将所有消息标记为已读吗？')) {
            $.post('{{ url_for("message.mark_all_as_read") }}', function(response) {
                if (response.success) {
                    location.reload();
                } else {
                    alert('操作失败: ' + response.message);
                }
            });
        }
    });
    
    // 单条消息标记为已读
    $('.message-item').click(function(e) {
        // 如果点击的是链接，不处理
        if ($(e.target).closest('a').length > 0) {
            return;
        }
        
        var messageId = $(this).data('message-id');
        var $item = $(this);
        
        $.post('{{ url_for("message.mark_as_read", message_id=0) }}'.replace('0', messageId), function(response) {
            if (response.success) {
                $item.removeClass('unread');
                $item.find('.fa-circle').remove();
                updateUnreadCount();
            }
        });
    });
    
    // 更新未读消息数量
    function updateUnreadCount() {
        $.get('{{ url_for("message.get_unread_count") }}', function(response) {
            $('.badge-primary, .badge-danger').each(function() {
                if ($(this).text().match(/^\d+$/)) {
                    $(this).text(response.count);
                    if (response.count == 0) {
                        $(this).hide();
                    } else {
                        $(this).show();
                    }
                }
            });
        });
    }
});
</script>
{% endblock %}